/// <reference name="MicrosoftAjax.debug.js"/>

OpenAjax.hub.registerLibrary("MicrosoftSampleDataGenComponent", "http://openajax.org/InteropFest10/SampleDataGenComponent/microsoft", "1.0");

Type.registerNamespace("SampleDataGenComponent");

SampleDataGenComponent.Quote = function(symbol, name, price) {
    /// <summary>Represents a stock quote.</summary>
    /// <field name="symbol" type="String">The stock symbol.</field>
    /// <field name="name" type="String">The name of the company.</field>
    /// <field name="price" type="Number">The price of the stock.</field>
    this.symbol = symbol;
    this.name = name;
    this.price = price;
}
SampleDataGenComponent.Quote.registerClass("SampleDataGenComponent.Quote");

SampleDataGenComponent.CorpList = function() {
    /// <summary>A live list of fake companies and their stock prices.</summary>
    SampleDataGenComponent.CorpList.initializeBase(this);
    this._running = false;
    this._corpList = [];
    this._intervalCookie = null;
    this._tickDelegate = Function.createDelegate(this, this._tick);
}
SampleDataGenComponent.CorpList.prototype = {
    get_corpList: function() {
        /// <value type="Array" elementType="SampleDataGenComponent.Quote">
        ///     The list of companies in the list.
        /// </value>
        return this._corpList;
    },
    get_running: function() {
        /// <value type="Boolean">True if the corp list is running.</value>
        return this._running;
    },
    set_running: function(value) {
	    if (this._running && !value) {
		    clearInterval(this._intervalCookie);
		    this._intervalCookie = null;
		    this._running = false;
		    this._raiseRunningStateChanged();
	    }
	    else if (!this._running && value) {
	        this._running = true;
		    this._raiseRunningStateChanged();
		    this._intervalCookie = setTimeout(this._tickDelegate, 150);
	    }
    },
    add_runningStateChanged: function(handler) {
        /// <summary>This event gets triggered when the running state of the corp list changes.</summary>
        this.get_events().addHandler("runningStateChanged", handler);
    },
    remove_runningStateChanged: function(handler) {
        this.get_events().removeHandler("runningStateChanged", handler);
    },
    add_quoteChanged: function(handler) {
        /// <summary>This event gets triggered when a quote changed.</summary>
        this.get_events().addHandler("quoteChanged", handler);
    },
    remove_quoteChanged: function(handler) {
        this.get_events().removeHandler("quoteChanged", handler);
    },
    addCorp: function(symbol, name, price) {
        /// <summary>Adds a new company to the list.</summary>
        /// <param name="symbol" type="String">The stock symbol.</param>
        /// <param name="name" type="String">The name of the company.</param>
        /// <param name="price" type="Number">The price of the stock.</param>
        this._corpList.push(new SampleDataGenComponent.Quote(symbol, name, price));
    },
    toggleRunningState: function() {
        /// <summary>Toggles the running state of the list.</summary>
        this.set_running(!this.get_running());
    },
    _raiseRunningStateChanged: function() {
        var handler = this._events.getHandler("runningStateChanged");
        if (handler) {
            handler(this, Sys.EventArgs.Empty);
        }
    },
    _tick: function() {
	    var inflation = 1.01;
	    var i = Math.floor(Math.random() * this._corpList.length);
	    var stock = this._corpList[i];
	    var oldprice = stock.price;
	    var oldprice_div10 = oldprice / 10;
	    var oldprice_div20 = oldprice / 20;
	    var price = oldprice + (Math.random() * oldprice_div10 * inflation) - oldprice_div20;
	    stock.price = price;
        var handler = this._events.getHandler("quoteChanged");
        if (handler) {
            handler(this, new SampleDataGenComponent.QuoteChangedEventArgs(stock, oldprice));
        }
		this._intervalCookie = setTimeout(this._tickDelegate, 150);
    }
}
SampleDataGenComponent.CorpList.registerClass("SampleDataGenComponent.CorpList", Sys.Component);

SampleDataGenComponent.QuoteChangedEventArgs = function(quote, previousPrice) {
    /// <param name="quote" type="SampleDataGenComponent.Quote"></param>
    /// <param name="previousPrice" type="Number"></param>
    SampleDataGenComponent.QuoteChangedEventArgs.initializeBase(this);
    this._quote = quote;
    this._previousPrice = previousPrice;
}
SampleDataGenComponent.QuoteChangedEventArgs.prototype = {
    get_previousPrice: function() {
        /// <value type="Number">The price of the stock before it changed.</value>
        return this._previousPrice;
    },
    get_quote: function() {
        /// <value type="SampleDataGenComponent.Quote">The quote that changed.</value>
        return this._quote;
    }
}
SampleDataGenComponent.QuoteChangedEventArgs.registerClass('SampleDataGenComponent.QuoteChangedEventArgs', Sys.EventArgs);
